package com.earnings.okhttputils.utils.ui.activity.user;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.User;
import com.earnings.okhttputils.utils.bean.UserRealnameData;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.utils.CommonUtil;

import java.util.Map;


/*
编辑用户信息-实名认证
 */

public class UserEditRealnameActivity extends GodLeftHandBaseActivity implements View.OnClickListener {

    View clearName,clearCard;

    EditText name,card;
    TextView phone,info1,info2,info3,info4;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int onCreateLayoutId() {
        return  R.layout.activity_user_edit_realname;
    }

    @Override
    protected void init() {
        setTitle("实名认证");
        name= (EditText) findViewById(R.id.name);
        card= (EditText) findViewById(R.id.card);
        phone= (TextView) findViewById(R.id.phone);
        info1= (TextView) findViewById(R.id.info1);
        info2= (TextView) findViewById(R.id.info2);
        info3= (TextView) findViewById(R.id.info3);
        clearName=findViewById(R.id.clearName);
        clearCard=findViewById(R.id.clearCard);

        clearName.setOnClickListener(this);
        clearCard.setOnClickListener(this);
        findViewById(R.id.sumbit).setOnClickListener(this);
        phone.setText( CommonUtil.getUser().getMobile());
        if(!CommonUtil.getUser().getIdcard_status().equals( "0" )){
            initStatus(true);
            loadRealname();
        }else{
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        }
    }


    @Override
    public void onClick(View v) {

        int i = v.getId();
        if (i == R.id.sumbit) {
            sumbitRealname();


        } else if (i == R.id.clearName) {
            name.setText( "" );

        } else if (i == R.id.clearCard) {
            card.setText( "" );

        }
    }


    public void initStatus(boolean isTrue){
if(isTrue){
    clearName.setVisibility(View.GONE);
    clearCard.setVisibility(View.GONE);
    info1.setVisibility(View.VISIBLE);
    info2.setVisibility(View.VISIBLE);
    info3.setVisibility(View.VISIBLE);
    name.setEnabled(false);
    card.setEnabled(false);
    findViewById(R.id.sumbit).setVisibility(View.GONE);
}
    }


    /*
             提交实名认证
              */
    public void sumbitRealname() {
        Map map = new HttpMap(getContext());
        if(name.getText().toString().equals( "" )){ToastUtils.showToast( getContext(),"请输入您的姓名" );return;}
        else if(card.getText().toString().equals( "" )){ToastUtils.showToast( getContext(),"请输入您的身份证号");return;}
        map.put( "realname",name.getText().toString() );
        map.put( "idcard",card.getText().toString() );

            OkHttpUtils.post().url( HttpUrl.USER_REALNAME_SUMBIT_URL ).params( map ).build().execute( new HttpObjectCallback<UserRealnameData>( getContext() ) {
                @Override
                public void onSuccess(UserRealnameData object, String msg) {
                    initStatus(true);
                    ToastUtils.showToast( getContext(), msg );
                    User user=CommonUtil.getUser();
                    user.setIdcard_status( "已认证" );
                    CommonUtil.commitUser( user );
                    finish();
                    setResult( Constant.USER_UPDATE );
                }

                @Override
                public void onFail(int code, String msg) {
                    ToastUtils.showToast( getContext(), msg );
                }

            } );


    }

    /*
            查询实名认证
             */
    public void loadRealname() {
        Map map = new HttpMap(getContext());


            OkHttpUtils.post().url( HttpUrl.USER_REALNAME_LOAD_URL ).params( map ).build().execute( new HttpObjectCallback<UserRealnameData>(getContext()) {
                @Override
                public void onSuccess(UserRealnameData object, String msg) {
                    name.setText( object.getRealname() );
                 //   phone.setText( object.getMobile() );
                    card.setText( object.getIdcard().substring( 0,3 )+"************"+object.getIdcard().substring( object.getIdcard().length()-3,object.getIdcard().length() ));
                   initStatus(true);
                  //  showToast( msg );
                }

                @Override
                public void onFail(int code, String msg) {
                    ToastUtils.showToast( getContext(),msg );
                }

            } );


    }
}
